<script setup>
import {
  Edit,
  Delete, User
} from '@element-plus/icons-vue'
import { ref } from 'vue'
const customers = ref([
  {   "id":"7",
    "customerName":"钱大妈",
    "customerAddress":"丽萨天下无敌萨迦天地总歇会",
    "customerPhone":"1234678",
    "customerEmail":"3526279811@qq.com",
    "postalCode":"Sc122323",
    "contactPerson":"我当时",
    "contactPhone":"21312412"
    ,"bankName":"天地银行",
    "bankAccount":"1312315",
    "status":"可用"
  }
])
const pageNum = ref(1)//当前页
const total = ref(20)//总条数
const pageSize = ref(3)//每页条数
const onSizeChange = (size) => {
  pageSize.value = size
  customerList();
}
//当前页码发生变化，调用此函数
const onCurrentChange = (num) => {
  pageNum.value = num
  customerList();
}
import {customerListService,updateCustomerService,AddCustomerService,deleteCustomerService} from "@/api/customer.js";
const customerName = ref('')
const customerList = async ()=>{
  let  params = {
    pageNum: pageNum.value,
    pageSize:pageSize.value,
    customerName:customerName.value?customerName.value:null,
  }
  let result =  await  customerListService(params);
  total.value =  result.data.total
  customers.value = result.data.items;
}
customerList();
import {Plus} from '@element-plus/icons-vue'
import {ElMessage, ElMessageBox} from "element-plus";
import {useTokenStore} from "@/Stores/token.js";
//控制抽屉是否显示
const visibleDrawer = ref(false)
//添加表单数据模型
const customerModel = ref({
  id:'',
  customerName: '',
  customerAddress: '',
  customerPhone: '',
  customerEmail:'',
  postalCode:'',
  contactPerson:'',
  contactPhone:'',
  bankName:'',
  bankAccount:'',
  status:''
})
const title =ref('')
const tokenStore = useTokenStore()
const updateCustomerEcho =  (row)=>{
  title.value = '修改客户'
  visibleDrawer.value = true
  customerModel.value.id = row.id
  customerModel.value.customerName = row.customerName
customerModel.value.customerAddress = row.customerAddress
  customerModel.value.customerPhone = row.customerPhone
  customerModel.value.customerEmail = row.customerEmail
  customerModel.value.postalCode = row.postalCode
  customerModel.value.contactPerson = row.contactPerson
  customerModel.value.contactPhone = row.contactPhone
  customerModel.value.bankName = row.bankName
  customerModel.value.bankAccount = row.bankAccount
customerModel.value.status = row.status
}
const updateCustomer = async (state)=>{
  customerModel.value.status = state
  let result =  await updateCustomerService(customerModel.value);
  ElMessage.success(result.data?result.data:'修改成功')
  customerList()
  visibleDrawer.value = false
}
const clearData = ()=>{
  customerModel.value.id = '';
  customerModel.value.customerName = '';
  customerModel.value.customerAddress = '';
  customerModel.value.customPhone = '';
  customerModel.value.customEmail = '';
  customerModel.value.postalCode = '';
  customerModel.value.contactPerson = '';
  customerModel.value.contactPhone = '';
  customerModel.value.bankName = '';
  customerModel.value.bankAccount = '';
  customerModel.value.status = '';
}
const addCustomer = async (state) => {
  customerModel.value.state = state
  let result = await AddCustomerService(customerModel.value)
  ElMessage.success(result.data?result.data:'添加成功')
  customerList()
  visibleDrawer.value = false
}
const deleteCustomer = (row) => {
  ElMessageBox.confirm(
      '确认删除该条客户吗?',
      '温馨提示',
      {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
      }
  )
      .then(async () => {
        let result = await deleteCustomerService(row.id);
        ElMessage({
          type: 'success',
          message: '确认删除',
        })
        customerList()
      })
      .catch(() => {
        ElMessage({
          type: 'info',
          message: '取消了删除',
        })
      })
}
</script>
<template>
  <el-card class="page-container">
    <template #header>
      <div class="header">
        <span>客户列表</span>
        <div class="extra">
          <el-button type="primary" @click="visibleDrawer=true;title='添加客户';clearData()">添加客户</el-button>
        </div>
      </div>
    </template>
    <el-form inline>
      <el-form-item label="客户名称:">
        <el-input  placeholder="请输入客户名" v-model="customerName"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="customerList">搜索</el-button>
        <el-button @click="customerName='';customerList()">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="customers" style="width: 100%">
      <el-table-column label="序号" width="100" type="index"> </el-table-column>
      <el-table-column label="客户名称" prop="customerName"></el-table-column>
      <el-table-column label="客户地址" prop="customerAddress"></el-table-column>
      <el-table-column label="客户电话" prop="customerPhone"></el-table-column>
      <el-table-column label="客户邮箱" prop="customerEmail"></el-table-column>
      <el-table-column label="客户邮编" prop="postalCode"></el-table-column>
      <el-table-column label="联系人" prop="contactPerson"></el-table-column>
      <el-table-column label="联系人电话" prop="contactPhone"></el-table-column>
      <el-table-column label="开户银行" prop="bankName"></el-table-column>
      <el-table-column label="开户账号" prop="bankAccount"></el-table-column>
      <el-table-column label="状态" prop="status"></el-table-column>
      <el-table-column label="操作" width="100">
        <template #default="{ row }">
          <el-button :icon="Edit" circle plain type="primary" @click="updateCustomerEcho(row)" ></el-button>
          <el-button :icon="Delete" circle plain type="danger" @click="deleteCustomer(row)"></el-button>
        </template>
      </el-table-column>
      <template #empty>
        <el-empty description="没有数据" />
      </template>
    </el-table>
    <el-pagination v-model:current-page="pageNum" v-model:page-size="pageSize" :page-sizes="[3, 5 ,10, 15]"
                   layout="jumper, total, sizes, prev, pager, next" background :total="total" @size-change="onSizeChange"
                   @current-change="onCurrentChange" style="margin-top: 20px; justify-content: flex-end" />

    <el-drawer v-model="visibleDrawer" :title="title" direction="rtl" size="50%">
      <!-- 添加文章表单 -->
      <el-form :model="customerModel" label-width="100px" >
        <el-form-item label="客户名称" >
          <el-input v-model="customerModel.customerName" placeholder="请输入客户名"></el-input>
        </el-form-item>
        <el-form-item label="客户地址" >
          <el-input v-model="customerModel.customerAddress" placeholder="请输入客户地址"></el-input>
        </el-form-item>
        <el-form-item label="客户电话" >
          <el-input v-model="customerModel.customerPhone" placeholder="请输入客户电话"></el-input>
        </el-form-item>
        <el-form-item label="客户邮箱" >
          <el-input v-model="customerModel.customerEmail" placeholder="请输入客户邮箱"></el-input>
        </el-form-item>
        <el-form-item label="客户邮编" >
          <el-input v-model="customerModel.postalCode" placeholder="请输入客户邮编"></el-input>
        </el-form-item>
        <el-form-item label="客户邮编" >
          <el-input v-model="customerModel.postalCode" placeholder="请输入客户邮编"></el-input>
        </el-form-item>
        <el-form-item label="客户联系人" >
          <el-input v-model="customerModel.contactPerson" placeholder="请输入联系人"></el-input>
        </el-form-item>
        <el-form-item label="客户联系人电话" >
          <el-input v-model="customerModel.contactPhone" placeholder="请输入联系人电话"></el-input>
        </el-form-item>
        <el-form-item label="客户开户账号" >
          <el-input v-model="customerModel.bankAccount" placeholder="请输入客户开户账号"></el-input>
        </el-form-item>
        <el-form-item label="客户开户银行" >
          <el-input v-model="customerModel.bankName" placeholder="请输入客户开户银行"></el-input>
        </el-form-item>
<!--        <el-form-item label="文章分类">-->
<!--          <el-select placeholder="请选择" v-model="articleModel.categoryId">-->
<!--            <el-option v-for="c in customers" :key="c.id" :label="c.customerName" :value="c.id">-->
<!--            </el-option>-->
<!--          </el-select>-->
<!--        </el-form-item>-->
        <el-form-item>
          <el-button type="primary" @click="title==='添加客户'?addCustomer('可用'):updateCustomer('可用')">可用</el-button>
          <el-button type="info" @click="title==='添加客户'?addCustomer('不可用'):updateCustomer('不可用')">不可用</el-button>
        </el-form-item>
      </el-form>
    </el-drawer>
  </el-card>

</template>

<style lang="scss" scoped>
.page-container {
  margin-left: 5px;
  min-height: 100%;
  box-sizing: border-box;

  .header {
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
}
</style>
